/*
    广告配置模块
 */


const CONFIG = {
    "wx865062b0f9284b6d": {
        long: "adunit-699ec1d914e4ac08",
        short: "adunit-699ec1d914e4ac08",
    },
    "wx865062b0f9284b6d-xigua": {
        long: "adunit-4aff4069b4da5b44",
        short: "adunit-4aff4069b4da5b44",
    },
    "wx865062b0f9284b6d-xigua-index": {
        long: "adunit-51458afc1e8b3eb5",
        short: "adunit-51458afc1e8b3eb5",
    },
    "wx4d2f240c9005c513": {
        long: "adunit-d9f813c7a2ae65a5",
        short: "adunit-d9f813c7a2ae65a5",
    }
}

export function getAdConfig(appid) {
    /*
        根据appid 获取广告配置
     */
    let config = {
        long: "adunit-412230a79c58c4d7",
        short: "adunit-412230a79c58c4d7",
    }

    if (appid && CONFIG[appid]) {
        config = CONFIG[appid]
    }

    return config
}

class ActivateVideoAd {
    constructor(appid, options) {
        this.appid = appid
        this.options = options
        if (wx.createRewardedVideoAd) {
            let config = getAdConfig(appid);
            if (options && options.long && options.short) {
                config = options
            }
            this.longVideoAd = wx.createRewardedVideoAd({
                adUnitId: config.long,
            });
            this.shortVideoAd = wx.createRewardedVideoAd({
                adUnitId: config.short,
            });
            this.longVideoAd.onError((err) => { });
            this.shortVideoAd.onError((err) => { });
        }
    }
    show(type = "short") {
        return new Promise((resolve, reject) => {
            let ad = type === "short" ? this.shortVideoAd : this.longVideoAd;
            ad.offClose();
            ad.onClose((res) => {
                if (res.isEnded) {
                    // 看完了广告
                    resolve(true);
                } else {
                    // 没看完
                    resolve(false);
                }
            });
            ad.load()
                .then(() => {
                    ad.show().catch((err) => {
                        getApp().mox.log.error("ad show error", err);
                        reject();
                    });
                })
                .catch((err) => {
                    getApp().mox.log.error("ad load error", err);
                    reject();
                });
        });
    }
    watchXiguaAd(adId) {
        getApp().$modules.reportMonitor(adId, 1)
        this.show().then((end) => {
            if (end) {
                // 跳转推文链接
                getApp().$modules.reportMonitor(adId + '-success', 1)
                getApp().$modules.advertise.goXiGua()
            } else {

            }
        }).catch((err) => {
            getApp().$modules.reportMonitor(adId + '-exception', 1)
            getApp().mox.log.error("广告功能异常", err);
            getApp().$modules.advertise.goXiGua()
        });
    }
}

class ActivateVideoAdPool {
    constructor() {
        this.pool = {}
    }
    register(id, config) {
        this.pool[id] = new ActivateVideoAd(getApp().mox.appid, config)
    }

    getActivateVideoAd(id) {
        return this.pool[id]
    }
}

export default {
    goXiGua() {
        /*
            跳转西瓜推文
         */
        // let url = "https://mp.weixin.qq.com/s/M-N5uRjChaqI5l4G58sAPQ"
        let url = "http://mp.weixin.qq.com/s?__biz=MzI0MzQ1NDg1Mg==&mid=100002547&idx=1&sn=93fada5705b2218bdd3b2c383482da34&chksm=696d8c185e1a050e8114b22ea5fbee6bf82f70ff6779ec3280ea45cd0b35c43700522fac357e#rd"
        wx.navigateTo({
            url: "/pages/web/web?url=" + encodeURIComponent(url)
        })
    },
    goWeb(url) {
        wx.navigateTo({
            url: "/pages/web/web?url=" + encodeURIComponent(url)
        })
    },
    toMaihuoMiniProgram: (path, appid) => {
        wx.navigateToMiniProgram({
            appId: appid || "wx07d71bd80d8b6ced",
            path: path,
            fail: function () {
                //
            }
        });
    },
    getAdConfig: getAdConfig,
    getActivateVideoAdPool() {
        return new ActivateVideoAdPool()
    },
    getActivateVideoAd(appid) {
        return new ActivateVideoAd(appid)
    },
    handleAdClick(ad, context) {
        /*
            处理广告点击
         */
        if (ad.adType == "maihuo") {
            if (ad.adId) {
                getApp().$modules.reportMonitor(ad.adId, 1)
            }
            if (ad.target == "web") {
                getApp().$modules.advertise.goWeb(ad.webUrl)
            }
            else {
                getApp().$modules.advertise.toMaihuoMiniProgram(ad.apppath, ad.appid)
            }
        }
        else if (ad.adType == "xigua") {
            if (context && context.ActivateVideoAd) {
                context.ActivateVideoAd.watchXiguaAd(ad.adId)
            }
        }
        else if (ad.adType == "videoAd") {
            //
        }
    }
};